package com.graduate.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.graduate.model.AcctType;
import com.graduate.model.Department;
import com.graduate.model.FundJournal;
import com.graduate.service.IFundJournalService;
import com.graduate.util.ResponseResult;

@Controller
@RequestMapping(value="/dataRecord")
public class DataRecordController {

	@Autowired
	public IFundJournalService fundJournalService;
	/**
	 * 数据录入页面
	 * @return
	 */
	@RequestMapping(value="/index")
	public String index() {
			return "dataRecord/dataEntry";
	}
	
	/**
	 * 数据录入
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/dataEntry")
	@ResponseBody
	public ResponseResult dataEntry(@RequestParam Map<String, Object> map){
		FundJournal fundJournal=new FundJournal();
		//设置id用uuid
		String uuid = UUID.randomUUID().toString();
		String id=uuid.replaceAll("-", ""); //去掉"-"符号
		fundJournal.setId(id);
		fundJournal.setIncomeExpense(Integer.parseInt((String) map.get("incomeExpense")));
		fundJournal.setAccttype((String)map.get("acctType"));
		SimpleDateFormat  fmt=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = fmt.parse((String) map.get("occurTime"));
			fundJournal.setOccurtime(date);
		} catch (ParseException e) {
			System.out.println("时间格式化出错！");
			e.printStackTrace();
		}
		fundJournal.setSource((String)map.get("source"));
		fundJournal.setMoney(BigDecimal.valueOf(Double.valueOf((String) map.get("money"))));
	    int row=fundJournalService.insertFundJournal(fundJournal);
		if(row>0){
		return   ResponseResult.createBySuccessCode();
		}
		return   ResponseResult.createByFailedCode();
	}
	
	
	/**
	 * 修改流水记录
	 * @param map
	 */
	@RequestMapping(value="/updateByPrimaryKey")
	@ResponseBody
	public ResponseResult updateByPrimaryKey(String  jsonStr ){
		try{
			FundJournal fundJournal = JSON.parseObject(jsonStr, FundJournal.class);
			return fundJournalService.updateByPrimaryKey(fundJournal);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseResult.createByFailedCode();
		}
	}
	/**
	 * 删除流水记录
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteByPrimaryKey")
	@ResponseBody
	public ResponseResult deleteByPrimaryKey(String id){
		
		try{
			return fundJournalService.deleteByPrimaryKey(id);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseResult.createByFailedCode();
		}
	}
	
	/**
	 * 查询所有账目类型
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/listAcctType")
	@ResponseBody
	public List<AcctType> listAcctType(@RequestParam Map<String,Object> map){		
		List<AcctType> list=fundJournalService.listAcctType(map);
		return list;
	}
	
	/**
	 * 查询所有账目来源
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/listSource")
	@ResponseBody
	public List<Department> listSource(){		
		List<Department> list=fundJournalService.listSource();
		return list;
	}

}
